﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace GPS_DB
{
    public partial class AskLineForm : Form
    {
        string lineName;

        private AskLineForm()
        {
            InitializeComponent();
        }

        public AskLineForm(string ln)
        {
            lineName = ln;
            InitializeComponent();
        }

        Form_DataBaseSVR MainForm;

        private void button1_Click(object sender, EventArgs e)
        {
            if (MainForm.myframe.LineArray.Count > 0)
            {
                byte[] tmparray = new byte[100];
                int i = ListBoxSend.SelectedIndex;
                if (i == -1) return;

                for (int k = 0; k < 30; k++)
                {
                    tmparray[k] = 0x20;
                }
                GPS_CMD.LINESTRUCT linedata1 = (GPS_CMD.LINESTRUCT)MainForm.myframe.LineArray[i];
                tmparray[0] = (byte)linedata1.mode;
                Encoding.Default.GetBytes(linedata1.linename, 0, linedata1.linename.Length, tmparray, 1);
                Encoding.Default.GetBytes(linedata1.disname, 0, linedata1.disname.Length, tmparray, 9);
                MainForm.myport.SendCmd(0x03, 0x4E, tmparray, 0, 30);
            }
            Close();
        }

        private void button_Force_Click(object sender, EventArgs e)
        {
            if (MainForm.myframe.LineArray.Count > 0)
            {
                byte[] tmparray = new byte[100];
                int i = ListBoxSend.SelectedIndex;
                if (i == -1) return;

                GPS_CMD.LINESTRUCT linedata1 = (GPS_CMD.LINESTRUCT)MainForm.myframe.LineArray[i];
                tmparray[0] = (byte)linedata1.mode;
                Encoding.Default.GetBytes(linedata1.linename, 0, linedata1.linename.Length, tmparray, 1);
                Encoding.Default.GetBytes(linedata1.disname, 0, linedata1.disname.Length, tmparray, 9);

                MainForm.myport.SendCmd(0x03, 0x4d, tmparray, 0, 30);
            }

            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void AskLineForm_Load(object sender, EventArgs e)
        {
            MainForm = (Form_DataBaseSVR)Owner;
            string title = (string)MainForm.mydata.FileNames[MainForm.mydata.FileIndex - 1];
            this.Text = Path.GetFileNameWithoutExtension(title);
            if (MainForm.myframe.LineArray.Count > 0)
            {
                ListBoxSend.Items.Clear();
                for (int i = 0; i < MainForm.myframe.LineArray.Count; i++)
                {
                    GPS_CMD.LINESTRUCT linedata1 = (GPS_CMD.LINESTRUCT)MainForm.myframe.LineArray[i];
                    ListBoxSend.Items.Add(linedata1.mode.ToString() + " " + linedata1.linename + " " + linedata1.disname);
                }
            }
            if (MainForm.AutoSelectLine)
            {
                byte[] tmparray = new byte[100];
                string name = GetLineName();
                for (int i = 0; i < MainForm.myframe.LineArray.Count; i++)
                {
                    GPS_CMD.LINESTRUCT linedata = (GPS_CMD.LINESTRUCT)MainForm.myframe.LineArray[i];
                    tmparray[0] = (byte)linedata.mode;
                    Encoding.Default.GetBytes(linedata.linename, 0, linedata.linename.Length, tmparray, 1);
                    Encoding.Default.GetBytes(linedata.disname, 0, linedata.disname.Length, tmparray, 9);

                    if (name == linedata.linename.Trim())
                    {
                        MainForm.myport.SendCmd(0x03, 0x4E, tmparray, 0, 30);
                        Close();
                        break;
                    }
                }
            }
        }

        private string GetLineName()
        {
            string str = lineName;
            str = Path.GetFileNameWithoutExtension(str);
            if (str.IndexOf(" ")>0)
            {
                str = str.Remove(str.IndexOf(" "));
            }
            if (str.IndexOf("(") > 0)
            {
                str = str.Remove(str.IndexOf("("));
            }
            if (str.IndexOf("（") > 0)
            {
                str = str.Remove(str.IndexOf("（"));
            }
            if (str.IndexOf("R") > 0)
            {
                str = str.Remove(str.IndexOf("R"));
            }
            if (str.IndexOf("r") > 0)
            {
                str = str.Remove(str.IndexOf("r"));
            }
            str = str.Trim();
            return str;
        }

    /////////////////////////////////////////////////////////////////////////////////
    }
}
